package siga.visao_academico;

import java.sql.SQLException;
import siga.banco.conexao;
import siga.visao_busca.buscaDisciplina;
import siga.visao_busca.buscaSalas_Laboratorios;
import siga.visao_busca.buscaTurma;
import siga.visao.index;
import siga.visao.verErros;

public class CadatroQuadroHorario extends javax.swing.JInternalFrame {
    static String cod_curso="";
    /** Creates new form CadatroQuadroHorario */
    public CadatroQuadroHorario() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpAula = new javax.swing.JPopupMenu();
        Apagar = new javax.swing.JMenuItem();
        jpSala = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jpLab = new javax.swing.JPopupMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        barraFerramentaFonecedor = new javax.swing.JToolBar();
        jButton3 = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        jpPrincipal = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfTurma = new javax.swing.JTextField();
        jtfFase = new javax.swing.JTextField();
        jtfTurno = new javax.swing.JTextField();
        jpAulas = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtAulas = new javax.swing.JTable();
        jpSalas = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtSalas = new javax.swing.JTable();
        jpLaboratorios = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtLaboratorio = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jtfSemestre = new javax.swing.JTextField();

        jpAula.setName("jpAula"); // NOI18N

        Apagar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/page_delete.png"))); // NOI18N
        Apagar.setText("Apagar");
        Apagar.setName("Apagar"); // NOI18N
        Apagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ApagarActionPerformed(evt);
            }
        });
        jpAula.add(Apagar);

        jpSala.setName("jpSala"); // NOI18N

        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/page_delete.png"))); // NOI18N
        jMenuItem1.setText("Apagar");
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jpSala.add(jMenuItem1);

        jpLab.setName("jpLab"); // NOI18N

        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/page_delete.png"))); // NOI18N
        jMenuItem2.setText("Apagar");
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jpLab.add(jMenuItem2);

        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Quadro de Horários");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/centr_custo_16x16.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        barraFerramentaFonecedor.setFloatable(false);
        barraFerramentaFonecedor.setRollover(true);
        barraFerramentaFonecedor.setName("barraFerramentaFonecedor"); // NOI18N

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton3.setText("Buscar");
        jButton3.setToolTipText("Busca somente Docentes");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(jButton3);

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/sair_32x32.png"))); // NOI18N
        btSair.setMnemonic('S');
        btSair.setText("Sair");
        btSair.setToolTipText("Sair da Janela");
        btSair.setFocusable(false);
        btSair.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSair.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSair.setName("btSair"); // NOI18N
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btSair);

        jpPrincipal.setName("jpPrincipal"); // NOI18N

        jLabel1.setText("Turma:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Fase/Mod:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Turno:");
        jLabel3.setName("jLabel3"); // NOI18N

        jtfTurma.setEditable(false);
        jtfTurma.setName("jtfTurma"); // NOI18N

        jtfFase.setEditable(false);
        jtfFase.setName("jtfFase"); // NOI18N

        jtfTurno.setEditable(false);
        jtfTurno.setName("jtfTurno"); // NOI18N

        jpAulas.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Aulas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        jpAulas.setName("jpAulas"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtAulas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Aula", "Segunda-Feira", "Terça-Feira", "Quarta-Feira", "Quinta-Feira", "Sexta-Feira"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtAulas.setComponentPopupMenu(jpAula);
        jtAulas.setEnabled(false);
        jtAulas.setName("jtAulas"); // NOI18N
        jtAulas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtAulasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtAulas);
        jtAulas.getColumnModel().getColumn(0).setMinWidth(50);
        jtAulas.getColumnModel().getColumn(0).setPreferredWidth(50);
        jtAulas.getColumnModel().getColumn(0).setMaxWidth(50);

        javax.swing.GroupLayout jpAulasLayout = new javax.swing.GroupLayout(jpAulas);
        jpAulas.setLayout(jpAulasLayout);
        jpAulasLayout.setHorizontalGroup(
            jpAulasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 796, Short.MAX_VALUE)
        );
        jpAulasLayout.setVerticalGroup(
            jpAulasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jpSalas.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Sala de Aula", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        jpSalas.setName("jpSalas"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jtSalas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Aula", "Segunda-Feira", "Terça-Feira", "Quarta-Feira", "Quinta-Feira", "Sexta-Feira"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtSalas.setComponentPopupMenu(jpSala);
        jtSalas.setEnabled(false);
        jtSalas.setName("jtSalas"); // NOI18N
        jtSalas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtSalasMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtSalas);
        jtSalas.getColumnModel().getColumn(0).setMinWidth(50);
        jtSalas.getColumnModel().getColumn(0).setPreferredWidth(50);
        jtSalas.getColumnModel().getColumn(0).setMaxWidth(50);

        javax.swing.GroupLayout jpSalasLayout = new javax.swing.GroupLayout(jpSalas);
        jpSalas.setLayout(jpSalasLayout);
        jpSalasLayout.setHorizontalGroup(
            jpSalasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 796, Short.MAX_VALUE)
        );
        jpSalasLayout.setVerticalGroup(
            jpSalasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jpLaboratorios.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Laboratórios", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        jpLaboratorios.setName("jpLaboratorios"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jtLaboratorio.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Aula", "Segunda-Feira", "Terça-Feira", "Quarta-Feira", "Quinta-Feira", "Sexta-Feira"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtLaboratorio.setComponentPopupMenu(jpLab);
        jtLaboratorio.setEnabled(false);
        jtLaboratorio.setName("jtLaboratorio"); // NOI18N
        jtLaboratorio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtLaboratorioMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jtLaboratorio);
        jtLaboratorio.getColumnModel().getColumn(0).setMinWidth(50);
        jtLaboratorio.getColumnModel().getColumn(0).setPreferredWidth(50);
        jtLaboratorio.getColumnModel().getColumn(0).setMaxWidth(50);

        javax.swing.GroupLayout jpLaboratoriosLayout = new javax.swing.GroupLayout(jpLaboratorios);
        jpLaboratorios.setLayout(jpLaboratoriosLayout);
        jpLaboratoriosLayout.setHorizontalGroup(
            jpLaboratoriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 796, Short.MAX_VALUE)
        );
        jpLaboratoriosLayout.setVerticalGroup(
            jpLaboratoriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
        );

        jLabel4.setText("Semestre:");
        jLabel4.setName("jLabel4"); // NOI18N

        jtfSemestre.setEditable(false);
        jtfSemestre.setName("jtfSemestre"); // NOI18N

        javax.swing.GroupLayout jpPrincipalLayout = new javax.swing.GroupLayout(jpPrincipal);
        jpPrincipal.setLayout(jpPrincipalLayout);
        jpPrincipalLayout.setHorizontalGroup(
            jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpPrincipalLayout.createSequentialGroup()
                .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jpLaboratorios, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpSalas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpPrincipalLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jtfTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfSemestre)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfFase)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jtfTurno, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jpAulas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jpPrincipalLayout.setVerticalGroup(
            jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpPrincipalLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpPrincipalLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jpPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jpPrincipalLayout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfFase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jpPrincipalLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfTurno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jpPrincipalLayout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfSemestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jpAulas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpSalas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpLaboratorios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.DEFAULT_SIZE, 820, Short.MAX_VALUE)
            .addComponent(jpPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        setVisible(false);
        dispose();
}//GEN-LAST:event_btSairActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        buscaTurma bt = new buscaTurma(null,true);
        bt.setLocation((index.tela.width-bt.getWidth())/2, (index.tela.height-bt.getHeight())/2);
        bt.setVisible(true);
        if(buscaTurma.isConfirmaTurma()){
            try {
                jtfTurma.setText(index.LRturma.get(0).toString());
//                jtfSemestre.setText(index.LRsemestre.get(0).toString());
                jtfFase.setText(index.LRfase.get(0).toString());
//                jtfTurno.setText(index.LRturno.get(0).toString());

                conexao.Query("select cod_curso from grade_turma where cod_emp=" + index.cod_emp + " and codigo=\"" + index.LRturma.get(0).toString() + "\"");
                conexao.rs.next();
                cod_curso = conexao.rs.getString("cod_curso").toString();

                gerenciaCampos();
                buscaAulas(cod_curso);
                buscaSalas();
                buscaLabs();
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
        }
        index.LRturma.clear();
//        index.LRsemestre.clear();
        index.LRfase.clear();
//        index.LRturno.clear();
}//GEN-LAST:event_jButton3ActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        
    }//GEN-LAST:event_formInternalFrameOpened

    @SuppressWarnings("static-access")
    private void jtAulasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtAulasMouseClicked
        String field;
        if(evt.getClickCount()==2){
            buscaDisciplina bd = new buscaDisciplina(null,true);
            bd.setLocation((index.tela.width-bd.getWidth())/2, (index.tela.height-bd.getHeight())/2);
            bd.bd_curso = cod_curso;
            bd.setVisible(true);
            if(!index.LRdisciplina.isEmpty()){
                field="";
                switch (jtAulas.getSelectedColumn()){
                    case 1:
                        field += "SEG="+index.LRdisciplina.get(0);
                    break;
                    case 2:
                        field += "TER="+index.LRdisciplina.get(0);
                    break;
                    case 3:
                        field += "QUA="+index.LRdisciplina.get(0);
                    break;
                    case 4:
                        field += "QUI="+index.LRdisciplina.get(0);
                    break;
                    case 5:
                        field += "SEX="+index.LRdisciplina.get(0);
                    break;
                }
                if(!index.LRdisciplina.isEmpty()){
                    conexao.Inserir("update grade_horario set "+field+" where cod_turma=\""+jtfTurma.getText()+"\" and aula="+jtAulas.getValueAt(jtAulas.getSelectedRow(), 0).toString());
                }
                index.LRdisciplina.clear();
                buscaAulas(cod_curso);
            }
        }
    }//GEN-LAST:event_jtAulasMouseClicked

    private void jtSalasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtSalasMouseClicked
        String field;
        if(evt.getClickCount()==2){
            buscaSalas_Laboratorios bsl = new buscaSalas_Laboratorios(null,true);
            bsl.setLocation((index.tela.width-bsl.getWidth())/2, (index.tela.height-bsl.getHeight())/2);
            bsl.filtro = "SALA";
            bsl.setVisible(true);
            if(!index.LRsalaselaboratorios.isEmpty()){
                field="";
                switch (jtSalas.getSelectedColumn()){
                    case 1:
                        field += "SEG="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 2:
                        field += "TER="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 3:
                        field += "QUA="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 4:
                        field += "QUI="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 5:
                        field += "SEX="+index.LRsalaselaboratorios.get(0);
                    break;
                }
                if(!index.LRsalaselaboratorios.isEmpty()){
                    conexao.Inserir("update grade_horario_salas set "+field+" where cod_turma=\""+jtfTurma.getText()+"\" and aula="+jtSalas.getValueAt(jtSalas.getSelectedRow(), 0).toString());
                }
                index.LRsalaselaboratorios.clear();
                buscaSalas();
            }
        }
    }//GEN-LAST:event_jtSalasMouseClicked

    private void jtLaboratorioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtLaboratorioMouseClicked
        String field;
        if(evt.getClickCount()==2){
            buscaSalas_Laboratorios bsl = new buscaSalas_Laboratorios(null,true);
            bsl.setLocation((index.tela.width-bsl.getWidth())/2, (index.tela.height-bsl.getHeight())/2);
            bsl.filtro = "LABORATÓRIOS";
            bsl.setVisible(true);
            if(!index.LRsalaselaboratorios.isEmpty()){
                field="";
                switch (jtLaboratorio.getSelectedColumn()){
                    case 1:
                        field += "SEG="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 2:
                        field += "TER="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 3:
                        field += "QUA="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 4:
                        field += "QUI="+index.LRsalaselaboratorios.get(0);
                    break;
                    case 5:
                        field += "SEX="+index.LRsalaselaboratorios.get(0);
                    break;
                }
                if(!index.LRsalaselaboratorios.isEmpty()){
                    conexao.Inserir("update grade_horario_labs set "+field+" where cod_turma=\""+jtfTurma.getText()+"\" and aula="+jtLaboratorio.getValueAt(jtLaboratorio.getSelectedRow(), 0).toString());
                }
                index.LRsalaselaboratorios.clear();
                buscaLabs();
            }
        }
    }//GEN-LAST:event_jtLaboratorioMouseClicked

    private void ApagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ApagarActionPerformed
        String field="";
        switch (jtAulas.getSelectedColumn()){
            case 1:
                field += "SEG=null";
            break;
            case 2:
                field += "TER=null";
            break;
            case 3:
                field += "QUA=null";
            break;
            case 4:
                field += "QUI=null";
            break;
            case 5:
                field += "SEX=null";
            break;
        }
        if(!index.LRdisciplina.get(0).equals("")){
            conexao.Inserir("update grade_horario set "+field+" where cod_turma=\""+jtfTurma.getText()+"\" and aula="+jtAulas.getValueAt(jtAulas.getSelectedRow(), 0).toString());
        }
        buscaAulas(cod_curso);
    }//GEN-LAST:event_ApagarActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        String field="";
        switch (jtAulas.getSelectedColumn()){
            case 1:
                field += "SEG=null";
            break;
            case 2:
                field += "TER=null";
            break;
            case 3:
                field += "QUA=null";
            break;
            case 4:
                field += "QUI=null";
            break;
            case 5:
                field += "SEX=null";
            break;
        }
        if(!index.LRdisciplina.get(0).equals("")){
            conexao.Inserir("update grade_horario_salas set "+field+" where cod_turma=\""+jtfTurma.getText()+"\" and aula="+jtSalas.getValueAt(jtSalas.getSelectedRow(), 0).toString());
        }
        buscaSalas();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        String field="";
        switch (jtAulas.getSelectedColumn()){
            case 1:
                field += "SEG=null";
            break;
            case 2:
                field += "TER=null";
            break;
            case 3:
                field += "QUA=null";
            break;
            case 4:
                field += "QUI=null";
            break;
            case 5:
                field += "SEX=null";
            break;
        }
        if(!index.LRdisciplina.get(0).equals("")){
            conexao.Inserir("update grade_horario_labs set "+field+" where cod_turma=\""+jtfTurma.getText()+"\" and aula="+jtLaboratorio.getValueAt(jtLaboratorio.getSelectedRow(), 0).toString());
        }
        buscaLabs();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void gerenciaCampos(){
        jtAulas.setEnabled(true);
        jtSalas.setEnabled(true);
        jtLaboratorio.setEnabled(true);
    }

    private void novo_aulas(){
        for (int x = 1; x <= 5; x++) {
            conexao.Inserir("insert into grade_horario (cod_turma, aula) values(\""+jtfTurma.getText()+"\","+x+")");
        }
        buscaAulas(cod_curso);
    }

    private void buscaAulas(String cod_curso){
        try {
            javax.swing.table.DefaultTableModel jt = (javax.swing.table.DefaultTableModel) jtAulas.getModel();
            for (int x = jtAulas.getRowCount() - 1; x >= 0; --x) {
                    jt.removeRow(x);
            }

            conexao.Query("select g.aula, gd.descricao as seg, gd1.descricao as ter, gd2.descricao as qua, gd3.descricao as qui, gd4.descricao as sex from grade_horario g " +
                    " left join grade_disciplina gd on gd.codigo=g.SEG " +
                    " left join grade_disciplina gd1 on gd1.codigo=g.TER and gd.cod_curso=gd1.cod_curso " +
                    " left join grade_disciplina gd2 on gd2.codigo=g.QUA and gd.cod_curso=gd2.cod_curso " +
                    " left join grade_disciplina gd3 on gd3.codigo=g.QUI and gd.cod_curso=gd3.cod_curso " +
                    " left join grade_disciplina gd4 on gd4.codigo=g.SEX and gd.cod_curso=gd4.cod_curso " +
                    " where g.cod_turma='"+jtfTurma.getText()+"' and gd.cod_curso="+cod_curso+" group by g.aula  order by g.aula");
            if(conexao.rs.first()){
                do{
                    jt.addRow(new Object[]{conexao.rs.getString("aula"),conexao.rs.getString("seg"),conexao.rs.getString("ter"),conexao.rs.getString("qua"),conexao.rs.getString("qui"),conexao.rs.getString("sex")});
                }while(conexao.rs.next());
            }else{
                novo_aulas();
            }
        }catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    private void novo_salas(){
        for (int x = 1; x <= 5; x++) {
            conexao.Inserir("insert into grade_horario_salas (cod_turma, aula) values(\""+jtfTurma.getText()+"\","+x+")");
        }
        buscaSalas();
    }

    private void buscaSalas(){
        try {
            javax.swing.table.DefaultTableModel jt = (javax.swing.table.DefaultTableModel) jtSalas.getModel();
            for (int x = jtSalas.getRowCount() - 1; x >= 0; --x) {
                    jt.removeRow(x);
            }
            conexao.Query("select g.aula, gd.descricao as seg, gd1.descricao as ter, gd2.descricao as qua, gd3.descricao as qui, gd4.descricao as sex from grade_horario_salas g " +
                    " left join salas_laboratorios gd on gd.codigo=g.SEG " +
                    " left join salas_laboratorios gd1 on gd1.codigo=g.TER " +
                    " left join salas_laboratorios gd2 on gd2.codigo=g.QUA " +
                    " left join salas_laboratorios gd3 on gd3.codigo=g.QUI " +
                    " left join salas_laboratorios gd4 on gd4.codigo=g.SEX " +
                    " where g.cod_turma='"+jtfTurma.getText()+"' order by g.aula");
            if(conexao.rs.first()){
                do{
                    jt.addRow(new Object[]{conexao.rs.getString("aula"),conexao.rs.getString("seg"),conexao.rs.getString("ter"),conexao.rs.getString("qua"),conexao.rs.getString("qui"),conexao.rs.getString("sex")});
                }while(conexao.rs.next());
            }else{
                novo_salas();
            }
        }catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }
    private void novo_labs(){
        for (int x = 1; x <= 5; x++) {
            conexao.Inserir("insert into grade_horario_labs (cod_turma, aula) values(\""+jtfTurma.getText()+"\","+x+")");
        }
        buscaLabs();
    }

    private void buscaLabs(){
        try {
            javax.swing.table.DefaultTableModel jt = (javax.swing.table.DefaultTableModel) jtLaboratorio.getModel();
            for (int x = jtLaboratorio.getRowCount() - 1; x >= 0; --x) {
                    jt.removeRow(x);
            }
            conexao.Query("select g.aula, gd.descricao as seg, gd1.descricao as ter, gd2.descricao as qua, gd3.descricao as qui, gd4.descricao as sex from grade_horario_labs g " +
                    " left join salas_laboratorios gd on gd.codigo=g.SEG " +
                    " left join salas_laboratorios gd1 on gd1.codigo=g.TER " +
                    " left join salas_laboratorios gd2 on gd2.codigo=g.QUA " +
                    " left join salas_laboratorios gd3 on gd3.codigo=g.QUI " +
                    " left join salas_laboratorios gd4 on gd4.codigo=g.SEX " +
                    " where g.cod_turma='"+jtfTurma.getText()+"' order by g.aula");
            if(conexao.rs.first()){
                do{
                    jt.addRow(new Object[]{conexao.rs.getString("aula"),conexao.rs.getString("seg"),conexao.rs.getString("ter"),conexao.rs.getString("qua"),conexao.rs.getString("qui"),conexao.rs.getString("sex")});
                }while(conexao.rs.next());
            }else{
                novo_labs();
            }
        }catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Apagar;
    private javax.swing.JToolBar barraFerramentaFonecedor;
    private javax.swing.JButton btSair;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPopupMenu jpAula;
    private javax.swing.JPanel jpAulas;
    private javax.swing.JPopupMenu jpLab;
    private javax.swing.JPanel jpLaboratorios;
    private javax.swing.JPanel jpPrincipal;
    private javax.swing.JPopupMenu jpSala;
    private javax.swing.JPanel jpSalas;
    private javax.swing.JTable jtAulas;
    private javax.swing.JTable jtLaboratorio;
    private javax.swing.JTable jtSalas;
    private javax.swing.JTextField jtfFase;
    private javax.swing.JTextField jtfSemestre;
    private javax.swing.JTextField jtfTurma;
    private javax.swing.JTextField jtfTurno;
    // End of variables declaration//GEN-END:variables

}
